package com.example.project.projectspringcloud.controller.front.user;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.RespBody;
import com.example.project.projectspringcloud.entity.user.Comment;
import com.example.project.projectspringcloud.entity.user.Evaluate;
import com.example.project.projectspringcloud.entity.user.User;
import com.example.project.projectspringcloud.service.user.UserCommentService;
import com.example.project.projectspringcloud.service.user.UserEvaluateService;
import com.example.project.projectspringcloud.utils.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户端评论Controller")
@Controller
@RequestMapping(value = "/comment")
public class CommentController {

    @Autowired
    private UserCommentService userCommentService;

    @Autowired
    private UserEvaluateService userEvaluateService;

    @ApiOperation(value = "评论列表", notes = "返回评论列表")
    @GetMapping(value = "/list")
    public String list(Page page, Comment comment, Model model) {
        List<Comment> comments = userCommentService.list(page, comment);
        model.addAttribute("page", new Page<Comment>(comments));
        model.addAttribute("comment", comment);
        return "user/comment/comment-list";
    }

    @ApiOperation(value = "API评论列表", notes = "获取评论数据")
    @GetMapping(value = "/api/list")
    @ResponseBody
    public RespBody list(Comment comment, Page page) {
        Map result = new HashMap();
        // 获取评论
        List<Comment> comments = userCommentService.list(page,
                new Comment(comment.getTarget(), Constants.ContentType.TYPE_VIDEO));
        result.put("comments", new Page<Comment>(comments));

        // 获取评论总数量
        result.put("commentCount", userCommentService.count(new Comment(comment.getTarget(), Constants.ContentType.TYPE_VIDEO)));

        // 获取评价
        List<Evaluate> evaluates = userEvaluateService.list(null,
                new Evaluate(comment.getTarget(), Constants.ContentType.TYPE_VIDEO));
        result.put("evaluates", evaluates);
        return RespBody.ok(result);
    }

    @ApiOperation(value = "API发布评论", notes = "用户发布评论API")
    @PostMapping(value = "/api/comment")
    @ResponseBody
    public RespBody comment(Comment comment) throws Exception {
        User currentUser = WebUtil.getCurrentUser();
        userCommentService.save(comment);
        return RespBody.ok(true);
    }
}
